import 'dart:convert';

import 'package:get/get.dart';

import 'package:http/http.dart' as http;

import 'package:image_picker_web/image_picker_web.dart';

import 'common.dart';

class ImageUploader {
  static Future uploadImage() async {
    print(555555);

    final mediaData = await ImagePickerWeb.getImageAsBytes();
    if (mediaData == null) return;

    final uri = Uri.parse('$bsurl/bbs/upload');
    print(122222);
    print(uri);
    final request = http.MultipartRequest('POST', uri)
      ..files.add(
        http.MultipartFile.fromBytes('file', mediaData, filename: 'upload.jpg'),
      );
    request.headers.addAll({'Authorization': myget('token') ?? ''});
    print(444444);
    loading();
    final response = await request.send();
    final responseData = await http.Response.fromStream(response);
    if (responseData.statusCode == 200) {
      final jsonData = jsonDecode(responseData.body);
      return jsonData['data']['url'];
    } else {
      print('请求失败: ${responseData.statusCode}');
    }
  }
}
